package io.xccit.common.exception;

import io.xccit.common.enums.ResultCodeEnum;
import io.xccit.common.result.AjaxResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * @author CH_ywx
 * @date 2023-06-23
 * @description 全局异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public AjaxResult<Object> error(Exception e){
        return AjaxResult.fail(null).message(e.getMessage());
    }

    /**
     * 处理自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(AuthCustomException.class)
    @ResponseBody
    public AjaxResult<Object> authException(AuthCustomException e){
        return AjaxResult.fail(null).code(e.getCode()).message(e.getMessage());
    }

    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public AjaxResult<Object> accessPermsException(AccessDeniedException e){
        return AjaxResult.fail(null).code(ResultCodeEnum.PERMISSION.getCode()).message("没有权限执行该操作");
    }
}
